package com.umeox.watch.moto.settings.ui;


import static com.umeox.watch.moto.settings.Constant.Units.ARRAY_ID_DISTANCE;
import static com.umeox.watch.moto.settings.Constant.Units.ARRAY_ID_TEMPERATURE;
import static com.umeox.watch.moto.settings.Constant.Units.KEY_UNIT_RESULT;
import static com.umeox.watch.moto.settings.Constant.Units.KEY_UNIT_SELETED;
import static com.umeox.watch.moto.settings.Constant.Units.REQUEST_CODE_DISTANCE;
import static com.umeox.watch.moto.settings.Constant.Units.REQUEST_CODE_TEMPERATURE;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.settings.Constant;
import com.umeox.watch.moto.settings.R;
import com.umeox.watch.moto.settings.bean.SettingInfo;
import com.umeox.watch.moto.settings.database.SettingDB;
import com.umeox.watch.moto.settings.database.SettingDao;
import com.umeox.watch.moto.settings.database.SettingDaoImpl;
import com.umeox.watch.moto.settings.utils.SPUtils;


public class UnitActivity extends ThemeAppCompatActivity implements View.OnClickListener {

    //    public static final String MMKV_KEY_DISTANCE = "distance";
    public static final String MMKV_KEY_TEMPERATURE = "temperature";
    private TextView mDistanceDescTv;
    private TextView mTemperatureDescTv;
    private String[] mUnitDistanceList;
    private String[] mUnitTemperatureList;
    private SettingInfo mSettingInfo;
    private SettingDao mSettingDao;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_unit_home);
        initView();
    }

    private void initView() {
        mUnitTemperatureList = getUnitArray(R.array.Unit_Temperature);
        mUnitDistanceList = getUnitArray(R.array.Unit_Distance);
        final View temperatureLayout = findViewById(R.id.container_unit_temperature);
        temperatureLayout.setVisibility(View.GONE);
        temperatureLayout.setOnClickListener(this);
        findViewById(R.id.container_unit_distance).setOnClickListener(this);
        mDistanceDescTv = findViewById(R.id.tv_distance_desc);
        mTemperatureDescTv = findViewById(R.id.tv_temperature_desc);
        mSettingDao = SettingDaoImpl.getInstance(this);
        mSettingInfo = mSettingDao.query();
        final int distance = mSettingInfo.getDistanceUnit();
        final int temperature = SPUtils.getInt(this,MMKV_KEY_TEMPERATURE, 0);
        mDistanceDescTv.setText(mUnitDistanceList[distance]);
        mTemperatureDescTv.setText(mUnitTemperatureList[temperature]);
    }

    private String[] getUnitArray(int unitArrayId) {
        return getResources().getStringArray(unitArrayId);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.container_unit_temperature) {
            startActForResult(ARRAY_ID_TEMPERATURE, REQUEST_CODE_TEMPERATURE, mTemperatureDescTv.getText().toString());
        } else if (v.getId() == R.id.container_unit_distance) {
            startActForResult(ARRAY_ID_DISTANCE, REQUEST_CODE_DISTANCE, mDistanceDescTv.getText().toString());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (RESULT_OK == resultCode && data != null) {
            final int stringExtra = data.getIntExtra(KEY_UNIT_RESULT, 0);
            if (requestCode == REQUEST_CODE_TEMPERATURE) {
                mTemperatureDescTv.setText(mUnitTemperatureList[stringExtra]);
                SPUtils.setInt(UnitActivity.this,MMKV_KEY_TEMPERATURE, stringExtra);

            } else if (requestCode == REQUEST_CODE_DISTANCE) {
                mDistanceDescTv.setText(mUnitDistanceList[stringExtra]);
                mSettingInfo.setDistanceUnit(stringExtra);
                ContentValues values = new ContentValues();
                values.put("_id", mSettingInfo.getId());
                values.put(SettingDB.Column.DISTANCE_UNIT, mSettingInfo.getDistanceUnit());
                mSettingDao.insert(values);
                getContentResolver().notifyChange(SettingDB.URI, null);
            }
        }
    }

    private void startActForResult(int unitArrayId, int requestCode, String optionSelected) {
        Intent intent = new Intent(this, UnitOptionActivity.class);
        intent.putExtra(Constant.Units.KEY_UNIT_ARRAY_ID, unitArrayId);
        intent.putExtra(KEY_UNIT_SELETED, optionSelected);
        startActivityForResult(intent, requestCode);
    }
}
